/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.ucm;

import assignment_cp3120.DAO.Factory.DAOFactory;
import assignment_cp3120.dataObjects.User;
import assignment_cp3120.login.LoginView;
import assignment_cp3120.user.registeruser.*;
import assignment_cp3120.startup.StartupView;

/**
 * 
 *  REGISTER USER USE-CASE
 * 
 * Precondition: register user view is shown
 * 
 * 1. User fills out the form and presses submit
 * 2. System validates the data
 * 3. System registers the new user
 * 4. System shows login view
 * <P>
 * 2.a.1 System detects that forms are not full
 * 2.a.2 System displays error message
 * <P>
 * 2.b.1 System detects that the username exists in database
 * .b.2 System displays error
 * <P>
 * 2.c.1 System detects that the password and confirm fields are the same
 * 2.c.2 System gives error and clears fields
 * 
 * Postcondition: Display is updated accordingly 
 * 
 * @author Ricbard Irons
 */
public class UCRegisterUser implements UseCaseController {
    private RegisterUserModel model;
    private RegisterUserView view;
    /**
     * Constructor for register user use case. Sets the model and view to the input values
     * @param model
     * @param view
     */
    public UCRegisterUser(RegisterUserModel model, RegisterUserView view) {
        this.model = model;
        this.view = view;
    }
    /**
     * starts the program, shows the GUI
     */
    public void run(){
        if(view.validateForm()){
            setModelToFormData();
            if(model.isUniqueUsername()){
                if(model.isPassConfirmEqual()){
                    DAOFactory.getDAOFactory().getUserDAO().addUser(model.getUser());
                    StartupView.getInstance().setContents(StartupView.getInstance().createFrame());
                    LoginView.getInstance().showGUI();
                }
                else{
                    StartupView.getInstance().errorMessageBox("Password and Comfim must be the same", "Error!");
                }
            } else {
                StartupView.getInstance().errorMessageBox("Username already exists please choose a new one", "Error!");
            }
        }
        else{
            StartupView.getInstance().errorMessageBox("Some inputs are not valid", "Not Valid!");
        }
    }
    /**
     * Takes data from the from and sets it to the model
     */
    private void setModelToFormData(){
        model.setFirstName(view.getName()[model.FIRSTNAME_POS]);
        model.setLastName(view.getName()[model.LASTNAME_POS]);

        String dobArray = null;
        dobArray = Integer.toString(view.getDOB()[model.DOB_YEAR_POS]) + "-"
                + Integer.toString(view.getDOB()[model.DOB_MONTH_POS]) + "-" +
                Integer.toString(view.getDOB()[model.DOB_DAY_POS]);

        model.setDOB(dobArray);

        model.setAddress(view.getAddress()[model.ADDRESS_POS]);
        model.setSuburb( view.getAddress()[model.SUBURB_POS]);
        model.setState(view.getAddress()[model.STATE_POS]);
        model.setPostcode(view.getAddress()[model.POSTCODE_POS]);

        model.setUsername(view.getUserPass()[model.USERNAME_POS]);
        model.setPassword(view.getUserPass()[model.PASSWORD_POS]);
        model.setConfirmPassword(view.getUserPass()[model.CONFIRM_POS]);

        model.setAccountType(User.AC_TYPE_USER);
    }
}
